/***************************************************************************
 *   Copyright (C) 2011 by The rPem3d developers                           *
 *   elseifkk@users.sf.net                                                 *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "math.h"

#include "barchart.h"

#include <qpixmap.h>
#include <qpainter.h>
#include <qimage.h>

BarChart::~BarChart() {
}

BarChart::BarChart ( QWidget *parent, const char *name )
: QWidget ( parent, name ) {
    setPaletteBackgroundColor("black");
    thePm=NULL;
    lastPm=NULL;
}

void BarChart::initBarChart(float max) {
    if (thePm==NULL) thePm = new QPixmap(size());
    if (lastPm==NULL) lastPm = new QPixmap(size());
    tmp_max=0;
    tmp_mean=0;
    tmp_count=0;
    thePm->fill("black");
    bitBlt(lastPm,0,0,thePm);
    if (max>0) ymax=max;
    update();
}

void BarChart::drawMeasure(QPainter *p) {
    p->setPen(border_col);
    p->drawLine(0,0,width(),0);
    p->setPen(bar_col);
    for (int k=8;k<height();k+=8) {
        p->drawLine(0,k,width(),k);
    }
}

float BarChart::getMean() {
    return tmp_mean/(float)tmp_count;
}
float BarChart::getMax() {
    return tmp_max;
}
void BarChart::resetMeanMax() {
    tmp_max=0;
    tmp_mean=0;
    tmp_count=0;
}

void BarChart::updateBarChart(float v, float max, float v2) {

    if (v>0) {
        tmp_count++;
        if (tmp_max<v) tmp_max=v;
        tmp_mean+=v;
    }
    if (max>0&&ymax>0&&ymax!=max) {
        double sy=ymax/max;
        int h=floor(height()*sy+0.5);
        QImage img=lastPm->convertToImage().smoothScale(width(),h);
        thePm->fill("black");
        bitBlt(thePm,1,0,&img);
        ymax=max;
    } else {
        bitBlt(thePm,1,0,lastPm);
    }

    QPainter p(thePm);

    p.setPen ( "black" );
    p.drawLine(0,0,0,height()-1);
    int h=v/ymax*size().height();
    if (h>0) {
        if (h>=height()) h=height()-1;
        p.setPen(hist_col);
        p.drawLine ( 0,0,0,h );
    }
    if (v2>0) {
        h=v2/ymax*size().height();
        if (h>0) {
            if (h>=height()) h=height()-1;
            p.setPen(hist_col2);
            p.drawLine ( 0,0,0,h );
        }
    }

    bitBlt(lastPm,0,0,thePm);
    update();
}

void BarChart::mousePressEvent ( QMouseEvent *e ) {
}

void BarChart::paintEvent(QPaintEvent *e) {
    bitBlt(this,0,0,thePm);
    QPainter p(this);
    drawMeasure(&p);
}
